import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// elementPlus按需导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const pathSrc = path.resolve(__dirname, 'src')

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      '@': pathSrc,
    },
  },
  plugins: [
    vue(),
    AutoImport({
      // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
      // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
      resolvers: [ElementPlusResolver()],
      dts: path.resolve(pathSrc, 'auto-imports.d.ts'),
    }),

    Components({
      resolvers: [ElementPlusResolver()],
      dts: path.resolve(pathSrc, 'components.d.ts'),
    })
  ],
  base: process.env.NODE_ENV === 'production' ? process.env.VITE_API_URL : './',
  server: {
    host: '0.0.0.0',
    port: 8080,
    open: true,
    cors: true, // 允许跨域
    proxy: {
      '/prod-api': {
        target: 'http://shbdc.com.cn/prod-api/',
        // target: 'http://47.113.145.9/prod-api/',
        ws: true,
        changeOrigin: true,
        rewrite: (path) => path.replace('/prod-api', ''),
      },
    },
  },
  build: {
    outDir: 'dist',
    minify: 'esbuild',
    sourcemap: false,
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${new Date().getTime()}.js`,
        chunkFileNames: `assets/[name].${new Date().getTime()}.js`,
        assetFileNames: `assets/[name].${new Date().getTime()}.[ext]`,
      },
    },
  }
})
